package TangramCore;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public abstract class Hladina{
    public AffineTransform at;
    public static final int CENTER = 0, TOP = 1, BOTTOM = 2, LEFT = 4, RIGHT = 8, DEFAULT = 16;
    public static final int VZHLED_DILKY = 0, VZHLED_OBRYS = 1, VZHLED_OBRYS_SUSPENDED = 2;
    public Rectangle relpos, pos;
    Obrazec obr;
    
  /*public void Hladina(Rectangle rp){
    relpos = rp;
  }*/
    abstract void clear(Graphics2D g);
    abstract public void show();
    abstract public void hide();
    abstract void draw(Graphics2D g);
    abstract void validate();
    
    public void resize(){
    }//po zmene pozice pro vypocet transformace atd.
    
    void setObrazec(Obrazec o){
        obr = o;
    }
    
    public void resize(Rectangle r){
        pos = new Rectangle();
        pos.x = relpos.width < 0 ? (int)(r.x + r.width * relpos.x / -100.0d): relpos.x;
        pos.y = relpos.height < 0 ? (int)(r.y + r.height * relpos.y / -100.0d): relpos.y;
        pos.width = relpos.width < 0 ? (int)(r.width * relpos.width / -100.0d): relpos.width;
        pos.height = relpos.height < 0 ? (int)(r.height * relpos.height / -100.0d): relpos.height;
    }
    
    static boolean addRect(Rectangle u, Rectangle r) {
        if (r == null || r.isEmpty())
            return false;
        if (u.isEmpty())
            u.setBounds(r);
        else
            u.add(r);
        return true;
    }
    
    static boolean addRect2D(Rectangle2D u, Rectangle2D r) {
        if (r == null || r.isEmpty())
            return false;
        if (u.isEmpty())
            u.setRect(r);
        else
            u.add(r);
        return true;
    }
    
    public static void addTransformedRect2D(Rectangle rectUpdate, Rectangle2D rect, AffineTransform t){
        if(rect==null || rect.isEmpty())
            return;
        addRect(rectUpdate, t.createTransformedShape(rect).getBounds()); //transform
        
    }
    
    public void addTransformedRect2D(Rectangle rectUpdate, Rectangle2D rect){
        //rect.setRect(rect.getX()-1,rect.getY()-1,rect.getWidth()+2,rect.getHeight()+2);
        //System.out.println(rectUpdate.isEmpty());
        addTransformedRect2D(rectUpdate, rect, at);
    }
    
    public java.awt.geom.Point2D transformPoint(double x, double y, java.awt.geom.Point2D p) {
        p.setLocation(x, y);
        try{
            at.inverseTransform(p, p);
        } catch (Exception e) {};
        return p;
    }
    public boolean contains(Point p){
        return pos.contains(p);
    }
    public void invalidate(){
        addRect2D(obr.rectUpdate, pos);
    }
}
